$(function(){
	var date=new Date();
	var dateTime=date.getFullYear()+"-"+appendzero(date.getMonth()+1)+"-"+appendzero(date.getDate());
	
	/**
	 * 如果是从菜单栏进入初始化判断
	 */
	if($(".take .date input").val()==""){
		if(date.getHours()>=20){//判断当前时间
			$(".take .date input").val(calTime(dateTime,1));
			$(".return .date input").val(calTime($(".take .date input").val(),2));
		}else{
			$(".take .date input").val(dateTime);
			$(".return .date input").val(calTime($(".take .date input").val(),2));
		}
	}
	/**
	 * 绑定取车时间事件
	 */
	$("select[name=takeTime]").change(function(){
		var qutime=$("select[name=takeTime] option:selected").val().split(":");
		var hour=qutime[0];
		var minute=qutime[1];
		var curhour=date.getHours();
		if(dateTime==$(".take .date input").val()){
			if(curhour>=20){
				layer.alert("夜太深，请预定明天吧");
				$(".take .date input").val(calTime(dateTime,1));
				return;
			}
			if(curhour<=9){
				curhour=7;
			}
			if(hour<curhour+2){
				layer.alert("请选择两小时后的时间！");
				$("select[name=takeTime] option").each(function(){
					var gettime=appendzero(curhour+2)+":"+minute;
					if($(this).val()==gettime){
						$(this).prop("selected","selected");
						$(this).siblings().removeProp("selected");
					};
				});
			}
		}
	});
	
	/**
	 * 取还车时间获取
	 */
	if($("select[name=takeTime]").attr("data-time")==""){
		$("select[name=takeTime] option").each(function(){
			var hours=date.getHours();
			if(hours>=20 && hours<=9){
				hours=7;
			}
			var curtime=appendzero(hours+2)+":00";
			if($(this).val()==curtime){
				$(this).attr("selected","selected");
			};
		});
	}else{
		$("select[name=takeTime] option").each(function(){
			var qutime=$("select[name=takeTime]").attr("data-time");
			if($(this).val()==qutime){
				$(this).attr("selected","selected");
			};
		});
		$("select[name=returnTime] option").each(function(){
			var huantime=$("select[name=returnTime]").attr("data-time");
			if($(this).val()==huantime){
				$(this).attr("selected","selected");
			};
		});
	}
	
	/**
	 * 门店地点动态改变
	 */
	$(".location").change(function(){
		var target=$(this);
		var dataId=target.find("option:selected").attr("data-id");
		$.get("updateaddress",{pid:dataId},function(data){
			target.siblings(".store").html("");
			$.each($.parseJSON(data), function(i, item) {
				var option=$("<option value="+item.psName+" data-id="+item.psId+">").text(item.psName);
				target.siblings(".store").append(option);
			});
		});
	});
	
	/**
	 * 确认修改按钮
	 */
	$(".address button").click(function(){
		var quDay=$("input[name=takeTime]").val();
		var quTime=$("select[name=takeTime] option:selected").text();
		var quLocation=$("select[name=qulocation] option:selected").attr("data-id");
		var quStore=$("select[name=qustore] option:selected").attr("data-id");
		var huanDay=$("input[name=returnTime]").val();
		var huanTime=$("select[name=returnTime] option:selected").text();
		var huanLocation=$("select[name=huanlocation] option:selected").attr("data-id");
		var huanStore=$("select[name=huanstore] option:selected").attr("data-id");
		var page=$(".dateTime").attr("data-page");
		//判断时间
		var qu=quDay+" "+$.trim(quTime)+":00";
		var huan=huanDay+" "+$.trim(huanTime)+":00";
		if(compareCurrent(qu) && compareDate(qu,huan)){
			if($(".out").attr("data-type")==2){
				window.location.href=page+"quday="+quDay+"&qutime="+quTime+"&qulocation="+quLocation+"&qustore="+quStore;
			}else{
				window.location.href=page+"quday="+quDay+"&qutime="+quTime+"&qulocation="+quLocation+"&qustore="+quStore+"&huanday="+huanDay+"&huantime="+huanTime+"&huanlocation="+huanLocation+"&huanstore="+huanStore;
			}
		}else{
			layer.alert("请选择有效时间！");
		}
	});
	
	
	/**
	 * 计算时间
	 * @returns
	 */
	function calTime(value,count){
		var date=new Date(Date.parse(value.replace(/-/g,"/")));
		date.setDate(date.getDate()+count);
		var year = date.getFullYear();
		var month = (date.getMonth()+1)<10?"0"+(date.getMonth()+1):(date.getMonth()+1);
		var day = date.getDate()<10?"0"+date.getDate():date.getDate();
		return year+"-"+month+"-"+day;
	}
	/**
	 * 补0
	 * @param obj
	 * @returns
	 */
	function appendzero (obj) {
		 if (obj < 10) return "0" + obj; else return obj;
	}
	
	/**
	 * 判断日期在今天之前还是之后,返回是否在当前时间之后
	 */
	function compareCurrent(d){
		var time=new Date(d.replace(/\-/g,'/'));
		return time>date;
	}
	/**
	 * 判断两个日期先后，返回前是否在后之前
	 * @param d1
	 * @param d2
	 * @returns
	 */
	function compareDate(d1,d2){
		var time1=new Date(d1.replace(/\-/g,'/'));
		var time2=new Date(d2.replace(/\-/g,'/'));
		return time1<time2;
	}
});
